﻿#include "CSFrame.h"
#include "MyFrame.h"

wxBEGIN_EVENT_TABLE(CSFrame, wxFrame)
EVT_COLOURPICKER_CHANGED(wxID_ANY, CSFrame::OnColorSelected)
wxEND_EVENT_TABLE()

CSFrame::CSFrame(const wxString& title, wxPanel* parent)
    : wxFrame(NULL, wxID_ANY, title, wxPoint(375, 200), wxSize(350, 216)) {
    m_parent = parent;
    wxPanel* panel1 = new wxPanel(this, wxID_ANY);
    wxPanel* panel2 = new wxPanel(this, wxID_ANY);
    wxPanel* panel3 = new wxPanel(this, wxID_ANY);

    // 第一个水平 sizer
    wxBoxSizer* horizontalSizer1 = new wxBoxSizer(wxHORIZONTAL);

    // 文本标签 "选择颜色"
    wxStaticText* label1 = new wxStaticText(panel1, wxID_ANY, "设置画笔颜色");
    horizontalSizer1->Add(label1, 0, wxALIGN_CENTER | wxALL, 10);

    // 颜色选择器1
    m_colourPicker1 = new wxColourPickerCtrl(panel1, ID_M_COLOURPICKER1, *wxBLACK);
    horizontalSizer1->Add(m_colourPicker1, 0, wxALIGN_CENTER | wxALL, 10);
   
    panel1->SetSizer(horizontalSizer1);
    panel1->Layout();

    // 第二个水平 sizer
    wxBoxSizer* horizontalSizer2 = new wxBoxSizer(wxHORIZONTAL);

    // 文本标签 "选择颜色"
    wxStaticText* label2 = new wxStaticText(panel2, wxID_ANY, "设置笔刷颜色");
    horizontalSizer2->Add(label2, 0, wxALIGN_CENTER | wxALL, 10);

    // 颜色选择器2
    m_colourPicker2 = new wxColourPickerCtrl(panel2, ID_M_COLOURPICKER2, *wxRED);
    horizontalSizer2->Add(m_colourPicker2, 0, wxALIGN_CENTER | wxALL, 10);
    
    //设置透明按钮
    setTrasparentButton2 = new wxButton(panel2, ID_SETTRANSPARENTBUTTON2, "透明", wxPoint(155, 100), wxDefaultSize);
    horizontalSizer2->Add(setTrasparentButton2, 0, wxALIGN_CENTER | wxALL, 10);

    panel2->SetSizer(horizontalSizer2);
    panel2->Layout();

    // 第三个垂直 sizer
    wxBoxSizer* verticalSizer1 = new wxBoxSizer(wxVERTICAL);
    
    //保存按钮
    yesButton = new wxButton(panel3, ID_YESBUTTON, "保存", wxPoint(155, 100), wxDefaultSize);
    verticalSizer1->Add(yesButton, 0, wxALIGN_CENTER | wxALL, 10);
    
    panel3->SetSizer(verticalSizer1);
    panel3->Layout();

    // 创建垂直布局管理器
    wxBoxSizer* verticalSizer = new wxBoxSizer(wxVERTICAL);
  
    // 将面板1和面板2添加到布局管理器
    verticalSizer->Add(panel1, 1, wxEXPAND | wxALL, 0);
    verticalSizer->Add(panel2, 1, wxEXPAND | wxALL, 0);
    verticalSizer->Add(panel3, 1, wxEXPAND | wxALL, 0);
    
    // 设置布局管理器为主窗口的布局管理器
    SetSizer(verticalSizer);


    Connect(ID_M_COLOURPICKER1, wxEVT_COMMAND_BUTTON_CLICKED,
        wxColourPickerEventHandler(CSFrame::OnColorSelected));
    
    Connect(ID_M_COLOURPICKER2, wxEVT_COMMAND_BUTTON_CLICKED,
        wxColourPickerEventHandler(CSFrame::OnColorSelected));

    Connect(ID_SETTRANSPARENTBUTTON2, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(CSFrame::SetBrushTransparent));

    Connect(ID_YESBUTTON, wxEVT_COMMAND_BUTTON_CLICKED,
        wxCommandEventHandler(CSFrame::CloseWindow));
}

void CSFrame::OnColorSelected(wxColourPickerEvent& event)
{
    selectedColor = m_colourPicker1->GetColour();
    selectedBrushColour = m_colourPicker2->GetColour();
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_mu->pencolor = selectedColor;
    myframe->m_mu->brushcolor = selectedBrushColour;
    wxPaintDC dc(this);
    dc.SetPen(wxPen(selectedColor, 5));
    dc.SetBrush(wxBrush(selectedBrushColour, wxBrushStyle::wxBRUSHSTYLE_SOLID));
}

void CSFrame::SetBrushTransparent(wxCommandEvent& event) {
    selectedBrushColour = -1;
    MyFrame* myframe = (MyFrame*)m_parent->GetParent();
    myframe->m_mu->brushcolor = selectedBrushColour;
    wxPaintDC dc(this);
    dc.SetBrush(wxBrush(selectedBrushColour, wxBrushStyle::wxBRUSHSTYLE_SOLID));
}

void CSFrame::CloseWindow(wxCommandEvent& event) {
    Close();
}